﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using PBMessage;

public class UISelectUnit : BaseWidget
{
    public GameObject UnitPrefab;
    public Transform GridT;
    public override  void OnLoad()
    {
        var UnitLists = Game.Inst.Table.UnitTable.Values;
        foreach (var unitTab in UnitLists)
        {
            Hashtable unitDic = (Hashtable)unitTab;
            if (unitDic.ContainsKey(":hero?")&& (bool)unitDic[":hero?"])
            {
                GameObject newItem = GameObject.Instantiate<GameObject>(UnitPrefab);
                newItem.transform.SetParent(GridT.transform, false);
                newItem.transform.Find("Name").GetComponent<Text>().text = (string)unitDic[":name"];
                newItem.SetActive(true);
                newItem.name = ((int)unitDic[":id"]).ToString();
            }
        }
    }

    public void OnSelectUnit(GameObject go)
    {
        Hashtable selUnit = (Hashtable) Game.Inst.Table.UnitTable[int.Parse(go.name)];
        CS_SelectUnit selectUnit = new CS_SelectUnit();
        selectUnit.UnitId = (int)selUnit[":id"];

        Game.Inst.Net.NetClient.Send((int)PBMesasgeID.CS_SelectUnit, selectUnit);
        UIManager.Inst.Close(this);
    }
}
